﻿using System.Windows;
using System.Windows.Controls;
using CodeAngel.UI.Extensions;
using System;
using System.Windows.Data;
using System.Windows.Input;

namespace CodeAngel.UI
{
    /// <summary>
    /// Interaction logic for LabeledComboBox.xaml
    /// </summary>
    public partial class LabeledComboBox : UserControl
    {
        public LabeledComboBox()
        {
            InitializeComponent();
            this.MyHorizontalLabeledControl.Bind(HorizontalLabeledControl.LabelProperty, "Label", this);
            this.MyHorizontalLabeledControl.Bind(HorizontalLabeledControl.LabelWidthProperty, "LabelWidth", this);
            this.MyComboBox.Bind(ComboBox.TextProperty, "Text", this, BindingMode.TwoWay);
            this.MyComboBox.Bind(ComboBox.ItemsSourceProperty, "ItemsSource", this);
            this.MyComboBox.Bind(ComboBox.SelectedItemProperty, "SelectedItem", this, BindingMode.TwoWay);
            this.MyComboBox.Bind(ComboBox.IsEditableProperty, "IsEditable", this);

            //this.Focusable = false;
            this.GotFocus += delegate
            {
                var FocussedElement = FocusManager.GetFocusedElement(FocusManager.GetFocusScope(this));
                if (FocussedElement.GetType() != typeof(TextBox)) 
                    this.MyComboBox.Focus();

            };
        }

        #region Label

        /// <summary>
        /// Gets or sets the Label.
        /// </summary>
        public string Label
        {
            get { return (string)GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for Label.
        /// </summary>
        public static readonly DependencyProperty LabelProperty =
            DependencyProperty.Register("Label", typeof(string), typeof(LabeledComboBox), new UIPropertyMetadata("label"));

        #endregion

        #region LabelWidth

        /// <summary>
        /// Gets or sets the LabelWidth.
        /// </summary>
        public double LabelWidth
        {
            get { return (double)GetValue(LabelWidthProperty); }
            set { SetValue(LabelWidthProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for LabelWidth.
        /// </summary>
        public static readonly DependencyProperty LabelWidthProperty =
            DependencyProperty.Register("LabelWidth", typeof(double), typeof(LabeledComboBox), new UIPropertyMetadata(double.NaN));

        #endregion

        #region Text

        /// <summary>
        /// Gets or sets the Text.
        /// </summary>
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for Text.
        /// </summary>
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(LabeledComboBox), new UIPropertyMetadata(string.Empty));

        #endregion

        #region SelectedItem

        /// <summary>
        /// Gets or sets the SelectedItem.
        /// </summary>
        public object SelectedItem
        {
            get { return (object)GetValue(SelectedItemProperty); }
            set { SetValue(SelectedItemProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for SelectedItem.
        /// </summary>
        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.Register("SelectedItem", typeof(object), typeof(LabeledComboBox), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        #endregion

		#region SelectionChanged

		/// <summary>
		/// Provides notification that the Selection has Changed.
		/// </summary>
		public event EventHandler SelectionChanged;

		/// <summary>
		/// Raises the SelectionChanged event.
		/// </summary>
		protected void RaiseSelectionChangedEvent(object sender, EventArgs e)
		{
		    if (this.SelectionChanged != null) this.SelectionChanged(this, EventArgs.Empty);
		}

		#endregion

        #region ItemsSource

        /// <summary>
        /// Gets or sets the ItemsSource.
        /// </summary>
        public object ItemsSource
        {
            get { return (object)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for ItemsSource.
        /// </summary>
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(object), typeof(LabeledComboBox), new UIPropertyMetadata(null));

        #endregion


        #region IsEditable

        /// <summary>
        /// Gets or sets the IsEditable.
        /// </summary>
        public bool IsEditable
        {
            get { return (bool)GetValue(IsEditableProperty); }
            set { SetValue(IsEditableProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for IsEditable.
        /// </summary>
        public static readonly DependencyProperty IsEditableProperty =
            DependencyProperty.Register("IsEditable", typeof(bool), typeof(LabeledComboBox), new UIPropertyMetadata(false));

        #endregion

        
    }
}
